package SpaceIT.components;

import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;

import model.dao.DongSanPhamDAO;
import model.dao.SanPhamDAO;
import model.pojo.DongSanPham;

import org.apache.tapestry5.*;
import org.apache.tapestry5.annotations.*;
import org.apache.tapestry5.ioc.*;
import org.apache.tapestry5.ioc.annotations.*;
import com.itextpdf.*;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import controller.util.ControllerHelper;

/**
 * Layout component for pages of application SpaceIT.
 */
@IncludeStylesheet("context:layout/layout.css")
public class Layout
{
	@Inject
    private Messages messages;
	
    /** The page title, for the <title> element and the <h1> element. */
    @Property
    @Parameter(required = true, defaultPrefix = BindingConstants.LITERAL)
    private String title;

    @Property
    @Parameter(defaultPrefix = BindingConstants.LITERAL)
    private Block headTag;
    
    @Property
    @Parameter(defaultPrefix = BindingConstants.LITERAL)
    private Block loadNAV;
    
    @Property
    @Parameter(defaultPrefix="literal")
    private DongSanPham dsp;
    
    @Property
    @Parameter(defaultPrefix = BindingConstants.LITERAL)
    private boolean showMenuLeft;
    
    @Property
    @Parameter(defaultPrefix = BindingConstants.LITERAL)
    private boolean showMenuRight;
    
    public boolean isDontShowMenu(){
//	{
    	if(!showMenuLeft && !showMenuRight)
    		return true;  
    	return false;
    }
    
    public Layout() {
//  {
		try 
		{
			showMenuLeft = true;
			showMenuRight = false;
			
			URI xmlPath = messages.getClass().getResource("connect.xml").toURI();
			URI propPath = messages.getClass().getResource("SP.properties").toURI();
			ControllerHelper.napThongTinKetNoi(xmlPath.getPath() , propPath.getPath());
		} 
		catch (URISyntaxException e) {}
	}
    
    @OnEvent(component = "atlBaoGia") 
	public StreamResponse onShowDetails() {
//	{		
    	try 
    	{
	    	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	        // Now we create the PDF
	
	        Document document = new Document();
        
			PdfWriter.getInstance(document, baos);
			document.open();
			PdfPTable table = new PdfPTable(5);
	        int size[] = {100,300,300,170,400} ;
	        table.setWidths(size);
	        table.addCell("MaSP");
	        table.addCell("TenSP");
	        table.addCell("Gia Ban");
	        table.addCell("Loai SP");
	        table.addCell("Mo Ta");
	        ArrayList<ArrayList<String>> arr = SanPhamDAO.BaoGia();
	        for (int i = 0; i < arr.size(); i++) 
	        {
	            ArrayList<String> g = arr.get(i);
	            table.addCell(g.get(0));
	            table.addCell(g.get(1));
	            table.addCell(g.get(2) + " VND");
	            table.addCell(g.get(3));
	            table.addCell(g.get(4));
	        }
	        document.add(table);
	        document.close();
	        // We write the PDF bytes to the OutputStream
	        InputStream is = new ByteArrayInputStream(baos.toByteArray());
	        
	        return new PDFStreamResponse(is,"Bao Gia");
		} 
    	catch (DocumentException e2) {}
    	return null;
	}

    public int getOp(){
//	
    	if(title.equals("Chi Tiet Laptop"))
			return 1;
		else
			return 0;
    }
    
    public boolean isLaptop()
    {
    	if(getOp() == 1)
		{
        	getDongSanPham();
    		return true;
		}
    	return false;
    }
    
    public ArrayList<DongSanPham> getDongSanPham()
    {
    	return DongSanPhamDAO.LayDongSanPham(getOp());
    }
}
